<html>
    <h2>Favorites Table</h2>
    <hr></hr>
    <br><br>
    <?php
        $file = 'urlList.txt';
        //$fileOpen = fopen($file, 'r');

        if (file_exists($file))
        {
            //reading the file into an array
            $url = file($file);

            echo '<table border = "1" cellpadding = "12">';
            echo '<tr><td><b>Link Name</b></td>';
            echo '<td><b>Category</b></td>';
            echo '<td><b>URL</b></td>';
            echo '<td><b>Date Added</b></td>';
            echo '<td><b>Function</b></td>';
            echo '<td><b>Times Visited</b></td></tr>';

            //looping through the users array and creating the appropriate associated array
            for ($i = 0; $i < count($url); $i++)
            {
                $temp = explode(', ', $url[$i]);

                //and creating the new array
                $url[$i] = array('linkName' => $temp[0], 'category' => $temp[1], 'url' => $temp[2], 'date' => $temp[3], 'counter' => $temp[4]);

                foreach ($url as $key => $value)
                {
                   echo '<tr><td>' . $temp[0] . '</td>';
                   echo '<td>' . $temp[1] . '</td>';
                   echo '<td>';
                   echo '<a href="counter.php?url=' . $temp[2] . '">' . $temp[0] . '</a>';
                   echo '</td>';
                   echo '<td>' . $temp[3] . '</td>';
                   echo '<td><form action="" method="post" name="deleteButton" id="deleteButton">';
                   echo '<p><a href="confirmDeleteLink.php?link=' .$temp[0].'"><b>Delete Link</b></a></p></td>';
                   echo '<td>' . $temp[4] . '</td></tr>';
                   break;
                }
            }
            echo '</table>';
        }
    ?>
        <br><br>
        <hr>
        <form action="main.php" method="post" name="favoritesTable" id="favoritesTable">
            <p><input type="submit" name="back" value="Go Back to Main Page" /></p>
        </form>
    
    <?
        if (isset ($_POST['back']))
        {
            header("Location: main.php");
        }
    ?>
    
    <?
        
        if (isset ($_POST['delete']))
        {
            $urlDoc = 'urlList.txt';

            $tempFile = 'tempURL.txt';

            if (file_exists($urlDoc))
            {
                $url = file($urlDoc);
                $urlFile = fopen($urlDoc, 'a+');

                for ($i = 0; $i < count($url); $i++)
                {
                    $temp = explode(', ', $url[$i]);

                    //and creating the new array
                    $url[$i] = array('linkName' => $temp[0], 'category' => $temp[1], 'url' => $temp[2], 'date' => $temp[3], 'counter' => $temp[4]);
                }
            }
        }
    ?>
</html>